﻿
/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class ExtensionsPage : WatiNBase
    {
        #region Constructors

        public ExtensionsPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public ExtensionsPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Links
        public Link InstallExtensionsWizardLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Install Extension Wizard"))); }
        }
        public Link UninstallPackageLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("UnInstall Package"));
                }
                return IEInstance.Link(Find.ByTitle("UnInstall Package"));
            }
        }
        public Link UninstallExtensionLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("UnInstall Extension"));
                }
                return IEInstance.Link(Find.ByTitle("UnInstall Extension"));
            }
        }

        public Link UnInstallReturnLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.StartsWith("dnn_ctr_UnInstall_cmdReturn")));
                }
                return IEInstance.Link(Find.ById(s => s.StartsWith("dnn_ctr_UnInstall_cmdReturn")));
            }
        }
        public Link WizardFinishLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("_ctr_Install_wizInstall_FinishNavigationTemplateContainerID_finishButtonStep")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("_ctr_Install_wizInstall_FinishNavigationTemplateContainerID_finishButtonStep")));
            }
        }
        public Link WizardNextLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Next"));
                }
                return IEInstance.Link(Find.ByTitle("Next"));
            }
        }
        public Link WizardStartLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_StartNavigationTemplateContainerID_nextButtonStart")));
                }
                return IEInstance.Link(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_StartNavigationTemplateContainerID_nextButtonStart")));
            }
        }
        public Link CoreLanguagePackSectionLink
        {
            get { return IEInstance.Link(Find.ByText("Core Language Packs")); }
        }
        public Link AuthenticationSystemsSectionLink
        {
            get { return IEInstance.Link(Find.ByText("Authentication Systems")); }
        }
        public Link PackageSettingsSectionLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByText("Package Settings"));
                }
                return IEInstance.Link(Find.ByText("Package Settings"));
            }
        }
        public Link ContainersSectionLink
        {
            get { return IEInstance.Link(Find.ByText("Containers")); }
        }
        public Link ExtensionLanguagePacksSectionLink
        {
            get { return IEInstance.Link(Find.ByText("Extension Language Packs")); }
        }
        public Link ModuleSectionLink
        {
            get { return IEInstance.Link(Find.ByText("Modules")); }
        }
        public Link CreateExtensionLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Create New Extension"))); }
        }
        public Link CreateModuleLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Create New Module"))); }
        }
        #endregion

        #region FileUploads
        public FileUpload SelectPackageDialog
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.FileUpload(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_cmdBrowse")));
                }
                return IEInstance.FileUpload(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_cmdBrowse")));
            }
        }
        #endregion

        #region CheckBoxes
        public CheckBox AcceptLicenseCheckBox
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_chkAcceptLicense")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("ctr_Install_wizInstall_chkAcceptLicense")));
            }
        }
        public CheckBox DeleteExtensionFilesCheckbox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("ctr_UnInstall_chkDelete")));
                }
                return ContentPane.CheckBox(Find.ById(s => s.EndsWith("ctr_UnInstall_chkDelete")));
            }
        }
        #endregion

        #region Divs
        public Div LicenseDiv
        {
            get { return ContentPane.Div(Find.ById(s => s.EndsWith("EditExtension_ctlExtension_fldLicense"))); }
        }
        public Div UnInstallResultsDiv
        {
            get { return IEInstance.Div(Find.ById(s => s.EndsWith("ctr_UnInstall_pnlUpload"))); }
        }
        public DivCollection ExtensionTypeDivs
        {
            get { return IEInstance.Divs.Filter(Find.ByClass("dnnForm exieContent dnnClear")); }
        }
        public Div InstalledExtensionDiv
        {
            get { return IEInstance.Div(Find.ById("installedExtensions")); }
        }
        public Div AvailableExtensionDiv
        {
            get { return IEInstance.Div(Find.ById("availableExtensions")); }
        }
        public Div MoreExtensionDiv
        {
            get { return IEInstance.Div(Find.ById("moreExtensions")); }
        }
        #endregion

        #region SelectLists
        public SelectList ExtensionPackageSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("Extensions_cboPackageTypes"))); }
        }
        public SelectList FilterByTypeSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("Extensions_cboPackageTypes"))); }
        }
        public SelectList ModuleCategorySelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("EditExtension_ModuleEditor_desktopModuleForm_category_category_ComboBox")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("EditExtension_ModuleEditor_desktopModuleForm_category_category_ComboBox")));
            }
        }
        #endregion

        #region Tables
        public Table ExtensionTable
        {
            get { return IEInstance.Table(Find.ById(s => s.EndsWith("Extensions_grdExtensions"))); }
        }
        public Table CoreLanguagePacksTable
        {
            get { 
                return ExtensionTypeDivs.Filter(Find.ByText(s => s.Contains("Core Language Packs")))[0].Table(
                        Find.ById(s => s.Contains("Extensions_installedExtensionsControl_extensionTypeRepeater_ctl03_extensionsGrid"))); }
        }
        public Table AuthenticationSystemsTable
        {
            get
            {
                return ExtensionTypeDivs.Filter(Find.ByText(s => s.Contains("Authentication Systems")))[0].Table(
                        Find.ById(s => s.Contains("Extensions_installedExtensionsControl_extensionTypeRepeater_ctl00_extensionsGrid")));
            }
        }
        public Table ContainersTable
        {
            get
            {
                return ExtensionTypeDivs.Filter(Find.ByText(s => s.Contains("Containers")))[0].Table(
                        Find.ById(s => s.Contains("Extensions_installedExtensionsControl_extensionTypeRepeater_ctl01_extensionsGrid")));
            }
        }
        public Table ExtensionLanguagePacksTable
        {
            get
            {
                return ExtensionTypeDivs.Filter(Find.ByText(s => s.Contains("Extension Language Packs")))[0].Table(
                        Find.ById(s => s.Contains("Extensions_installedExtensionsControl_extensionTypeRepeater_ctl04_extensionsGrid")));
            }
        }
        public Table ModulesTable
        {
            get
            {
                return ExtensionTypeDivs.Filter(Find.ByText(s => s.Contains("Modules")))[0].Table(
                        Find.ById(s => s.Contains("Extensions_installedExtensionsControl_extensionTypeRepeater_ctl06_extensionsGrid")));
            }
        }
        #endregion

        #region Spans
        public Span PackageNameSpan
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Span(Find.ById(s => s.EndsWith("EditExtension_packageFormReadOnly_DnnFormLiteralItem1_DnnFormLiteralItem1_Label")));
                }
                return IEInstance.Span(Find.ById(s => s.EndsWith("EditExtension_packageFormReadOnly_DnnFormLiteralItem1_DnnFormLiteralItem1_Label")));
            }
        }
        #endregion

        #region Buttons
        public Button ConfirmButton
        {
            get { if(PopUpFrame != null)
            {
                return PopUpFrame.Div(Find.ByClass(s => s.StartsWith("ui-dialog ui-widget"))).Button(
                        Find.ByText(s => s.Contains("Yes")));
            }
                return
                    IEInstance.Div(Find.ByClass(s => s.StartsWith("ui-dialog ui-widget"))).Button(
                        Find.ByText(s => s.Contains("Yes")));
            }
        }
        #endregion

        #region TextFields
        public TextField FolderNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("EditExtension_ModuleEditor_desktopModuleForm_folderName_folderName_TextBox")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("EditExtension_ModuleEditor_desktopModuleForm_folderName_folderName_TextBox")));
            }
        }
        public TextField BusinessControllerClassField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s =>s.EndsWith(
                                    "EditExtension_ModuleEditor_desktopModuleForm_controllerClass_controllerClass_TextBox")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith(
                                    "EditExtension_ModuleEditor_desktopModuleForm_controllerClass_controllerClass_TextBox")));
            }
        }
        public TextField DependenciesField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s =>s.EndsWith(
                        "EditExtension_ModuleEditor_desktopModuleForm_dependencies_dependencies_Textbox")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith(
                    "EditExtension_ModuleEditor_desktopModuleForm_dependencies_dependencies_Textbox")));
            }
        }
        public TextField PermissionsField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith(
                                    "EditExtension_ModuleEditor_desktopModuleForm_permissions_permissions_TextBox")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith(
                                    "EditExtension_ModuleEditor_desktopModuleForm_permissions_permissions_TextBox")));
            }
        }
        
        
        
        #endregion

        #endregion

        #region Public Methods

        public void InstallExtension(string packagePath)
        {
            if (!ModuleTitleSpan.Text.Contains("Install Extension"))
            {
                InstallExtensionsWizardLink.Click();
            }
            System.Threading.Thread.Sleep(1500);
            UploadExtensionsPackage(packagePath);
            WizardStartLink.ClickNoWait();
            IEInstance.WaitForComplete();
            WizardNextLink.ClickNoWait();
            IEInstance.WaitForComplete();
            WizardNextLink.ClickNoWait();
            IEInstance.WaitForComplete();
            AcceptLicenseCheckBox.ClickNoWait();
            WizardNextLink.ClickNoWait();
            IEInstance.WaitForComplete();
            WizardFinishLink.Click();
        }

        public Image GetExtensionEditButton(string extensionName)
        {
            //Returns the Delete Button for the user specified 
            Image result = null;
            foreach (TableRow row in ExtensionTable.TableRows)
            {
                if (row.TableCells[3].InnerHtml.Contains(extensionName))
                {
                    result = row.Image(Find.ByTitle("Edit this Extension"));
                    break;
                }
                continue;
            }
            return result;
        }

        public Image GetExtensionEditButton(string extensionName, Table extensionTypeTable)
        {
            //Returns the Delete Button for the user specified 
            Image result = null;
            foreach (TableRow row in extensionTypeTable.TableRows)
            {
                if (row.TableCells[3].InnerHtml.Contains(extensionName))
                {
                    result = row.Image(Find.ByTitle("Edit this Extension"));
                    break;
                }
                continue;
            }
            return result;
        }

        public TableRow GetExtensionRow(string extensionName)
        {
            //Returns the Delete Button for the user specified 
            TableRow result = null;
            foreach (TableRow row in ExtensionTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(extensionName))
                {
                    result = row;
                    break;
                }
                continue;
            }
            return result;
        }
        
        public Link GetExtensionUninstallButton(string extensionName, Table extensionTypeTable)
        {
            //Returns the Delete Button for the user specified 
            Link result = null;
            foreach (TableRow row in extensionTypeTable.TableRows)
            {
                if (row.TableCells[3].InnerHtml.Contains(extensionName))
                {
                    result = row.Link(Find.ByTitle("UnInstall this Extension"));
                    break;
                }
                continue;
            }
            return result;
        }

        public Image GetExtensionUninstallButton(string extensionName)
        {
            //Returns the Delete Button for the user specified 
            Image result = null;
            foreach (TableRow row in ExtensionTable.TableRows)
            {
                if (row.TableCells[3].InnerHtml.Contains(extensionName))
                {
                    result = row.Image(Find.ByTitle("Uninstall this Extension"));
                    break;
                }
                continue;
            }
            return result;
        }

        public void UploadExtensionsPackage(string packagePath)
        {
            SelectPackageDialog.Set(packagePath);
        }

        #endregion
    }
}
